<?php
include('../config.php');
include(PUNG_ROOT . '/admin/header.php');
include(PUNG_ROOT . '/system/class/gd.class.php');

$adminmenu = array(
LANG_045 => 'images.php'
);

if(isset($_POST['submit']) && $_FILES['file']['size'] != 0)
{
	$allowed = array('image/gif', 'image/jpeg', 'image/png', 'image/x-png', 'image/pjpeg');
	
	if(!in_array($_FILES['file']['type'], $allowed))
	{
		redirect_with_message('error', 'LANG_082');
	}
	
	$file = basename($_FILES['file']['name']);
	$file = filenameCleanup($file);
	$file = rand() . '_' . $file;
	
	$uproot = PUNG_ROOT . '/data/';
	$path = $uproot . $file;
	
	move_uploaded_file($_FILES['file']['tmp_name'], $path);
	
	$gd = new GD($path);
	$gd -> Resize('160', '160');
	$path_thumb = $uproot . 'thumbs/' . $file;
	$gd -> Save($path_thumb, '100%');
	
	$sql = $db -> db_query("INSERT INTO " . DB_PREFIX . "images (filename, filesize) VALUES (
	'$file',
	'" . $_FILES['file']['size'] . "'
	)");
	
	redirect_with_message('msg', 'LANG_083');
}

if($_GET['delete'])
{
	$filename = $_GET['delete'];
	$sql = $db -> db_query("DELETE FROM " . DB_PREFIX . "images 
	WHERE filename = '$filename'");
	
	unlink(PUNG_ROOT . '/data/' . $filename);
	unlink(PUNG_ROOT . '/data/thumbs/' . $filename);

	redirect_with_message('msg', 'LANG_088');
}

include(PUNG_ROOT . '/admin/head.php');
?>

<h3><?php print LANG_080; ?></h3>

<form enctype="multipart/form-data" method="post">
<table width="100%">
<tr>
	<td align="center"><input type="file" name="file" /></td>
</tr>
<tr>
	<td align="center"><input type="submit" name="submit" class="form_button" value="<?php print LANG_081; ?>" /></td>
</tr>
</table>
</form>

<h3><?php print LANG_045; ?></h3>

<?php
if(!isset($_GET['start']))
{ $start = 0; }
else 
{ $start = $_GET['start']; }
$eu = ($start - 0);
$limit = 5;
$current = $eu + $limit;
$back = $eu - $limit;
$next = $eu + $limit;

$sql = $db -> db_query("SELECT * FROM " . DB_PREFIX . "images");
$nume = $db -> db_rows();

$sql = $db -> db_query("SELECT * FROM " . DB_PREFIX . "images ORDER BY id DESC LIMIT $eu, $limit");
$sql = $db -> db_result();
?>

<form name="form">
<table width="100%">
<tr>
	<th><?php print LANG_084; ?></th>
	<th><?php print LANG_085; ?></th>
	<th><?php print LANG_086; ?></th>
	<th>&nbsp;</th>
</tr>
<?php foreach($sql as $image) { ?>
<tr>
	<td width="160"><a href="<?php print PUNG_URL . '/data/' . $image['filename']; ?>" 
	target="_blank"><img src="<?php print PUNG_URL . '/data/thumbs/' . $image['filename']; ?>"></a></td>
	<td><?php print convert_bytes($image['filesize']); ?></td>
	<td width="300">
	<input type="text" value="<?php print PUNG_URL . '/data/' . $image['filename']; ?>" style="width: 280px"
	onclick="javascript:this.form.pic<?php print $image['id']; ?>.focus();
	this.form.pic<?php print $image['id']; ?>.select();" name="pic<?php print $image['id']; ?>" />
	</td>
	<td width="50" align="center">
	<a href="?delete=<?php print $image['filename']; ?>" 
	title="<?php print LANG_087; ?>"><img 
	src="../images/admin_img/uninstall.png" alt="<?php print LANG_087; ?>" /></a>
	</td>
</tr>
<?php } ?>
</table>
</form>

<?php
// Pagination
print '<p align="center">';

if($back >= 0)
{
	print "<a href=\"?start=$back\">&lt; " . LANG_089 . "</a>&nbsp;&nbsp;";
}

$i=0;
$l=1;
for($i=0; $i < $nume;$i=$i+$limit)
{
	if($i <> $eu)
	{
		print "&nbsp;<a href=\"?start=$i\" style=\"text-decoration: none\">$l</a>";
	}
	else 
	{
		$all = round($nume / 4);
		print "&nbsp;<strong>( $l )</strong>";
	}
$l=$l+1;
}

if($current < $nume)
{
	print "&nbsp;&nbsp;<a href=\"?start=$next\">" . LANG_090 . " &gt;</a> ";
}

print '</p>';

include(PUNG_ROOT . '/admin/footer.php');
?>